package bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import com.ecdc.soexample.App;
import com.ecdc.soexample.HomeActivity;

import org.xutils.ex.DbException;
import java.lang.reflect.Method;
import java.util.List;
import enty.HealthSportEnty;
import ota.Myhex;
import utils.DbUitls;
import utils.ToastUtils;

public class GattUpdateReceiver extends BroadcastReceiver {
    public static BluetoothGattCharacteristic bluetoothGattCharacteristicUUID_NOTIFY_NOTIFY, bluetoothGattCharacteristicUUID_NOTIFY_WRITE_NO_RESPONSE;
//    public static BluetoothGattCharacteristic bluetoothGattCharacteristicUUID_NOTIFY_NOTIFY_OTA, bluetoothGattCharacteristicUUID_NOTIFY_WRITE_NO_RESPONSE_OTA;
    public static BluettothInterface mBluettothInterface;


    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {

            mBluettothInterface.updateBluetoothStatus("蓝牙连接");

        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mBluettothInterface.updateBluetoothStatus("蓝牙断开");

        } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {

            if (MyServiceConnection.mBluetoothLeService != null) {
                final List<BluetoothGattService> listBluetoothGattService_OTA = MyServiceConnection.mBluetoothLeService.getSupportedGattService(SampleGattAttributes.UUID_SERVICE_DATA);

                BluetoothGattService mBluetoothGattService = null;
                BluetoothGattService mBluetoothGattServiceOta = null;

                if (bluetoothGattCharacteristicUUID_NOTIFY_NOTIFY == null) {
                    if (listBluetoothGattService_OTA==null||listBluetoothGattService_OTA.size()<=0) {
                        Toast.makeText(App.context, "未搜索到蓝牙服务", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    //设置mtu值
                    boolean dddd = MyServiceConnection.mBluetoothLeService.setMTU(500);
                    mBluetoothGattServiceOta=listBluetoothGattService_OTA.get(0);
//
                    //OTA的特征值
                    BluetoothGattCharacteristic mBluetoothGattCharacteristicUUID_NOTIFY_NOTIFY = mBluetoothGattServiceOta.getCharacteristic(SampleGattAttributes.UUID_NOTIFY_NOTIFY_DATA);
                    BluetoothGattCharacteristic mBluetoothGattCharacteristicUUID_NOTIFY_WRITE_NO_RESPONSE= mBluetoothGattServiceOta.getCharacteristic(SampleGattAttributes.UUID_NOTIFY_WRITE_NO_RESPONSE_DATA);
                    bluetoothGattCharacteristicUUID_NOTIFY_NOTIFY = mBluetoothGattCharacteristicUUID_NOTIFY_NOTIFY;
                    bluetoothGattCharacteristicUUID_NOTIFY_WRITE_NO_RESPONSE = mBluetoothGattCharacteristicUUID_NOTIFY_WRITE_NO_RESPONSE;


                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // 设置这个可以接收到消息
                    boolean isS3 = MyServiceConnection.mBluetoothLeService.setCharacteristicNotificationToBliutooth2(bluetoothGattCharacteristicUUID_NOTIFY_NOTIFY, true);

                    mBluettothInterface.ddd();

                }



            }
        } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {

            String responceContent2 = intent.getStringExtra(BluetoothLeService.EXTRA_DATA).trim();
            System.out.println("蓝牙回复数据：" + responceContent2 + "    ");
            mBluettothInterface.bluetoothResponse(responceContent2);


            // 收到蓝牙回复的数据就开始发送后续的包数据--后续需要增加蓝牙回复正确的数据才可以继续发送下一包数据
            // handler.sendEmptyMessageDelayed(0, 500);

        } else if (BluetoothLeService.EXTRA_DATA_MTU.equals(action)) {

            String valus = intent.getStringExtra(BluetoothLeService.EXTRA_DATA_MTU);
            mBluettothInterface.getCurrentMtu(valus);
        } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { // 蓝牙状态广播

            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            switch (state) {
                case BluetoothAdapter.STATE_OFF:
                    mBluettothInterface.updateBluetoothStatus("蓝牙关闭");
                    ToastUtils.getToastUtilsInstance().toastCenterStrip("蓝牙关闭",HomeActivity.mHomeActivity,Color.RED);

                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
//                    Toast.makeText(App.context,"STATE_TURNING_OFF",Toast.LENGTH_LONG).show();
                    break;
                case BluetoothAdapter.STATE_ON:
//                        alertDialog2.dismiss();
//                    Toast.makeText(App.context,"STATE_ON",Toast.LENGTH_LONG).show();
                    break;
                case BluetoothAdapter.STATE_TURNING_ON:
                    ToastUtils.getToastUtilsInstance().toastCenterStrip("蓝牙打开",HomeActivity.mHomeActivity,Color.RED);
//                    Toast.makeText(App.context,"STATE_TURNING_ON",Toast.LENGTH_LONG).show();
                    mBluettothInterface.updateBluetoothStatus("蓝牙打开");

                    break;
            }

        } else if (BluetoothLeService.ACTION_NOTIFICATION.equals(action)) {//应用消息通知，例如qq、微信消息
            Bundle mBundle = intent.getBundleExtra(BluetoothLeService.ACTION_NOTIFICATION);


            String appPackageName = mBundle.getString("appPackageName");
            String appNotificationTitle = mBundle.getString("appNotificationTitle");
            String appNotificationText = mBundle.getString("appNotificationText");
            Toast.makeText(App.context, "应用通知消息：\n" + appNotificationText, Toast.LENGTH_LONG).show();


            if (appPackageName.equalsIgnoreCase(NotifacationApplicationPackageName.QQ)) {


            } else if (appPackageName.equalsIgnoreCase(NotifacationApplicationPackageName.WECHAT)) {

            } else {
                BluetoothCommandCode.getCheckSendDataUtilsInstance().sendApplicationInfomationToWatch(mBluettothInterface, Myhex.hexStringToByte(Myhex.unicodeEncode(appNotificationText)));
            }
//
//            new Handler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    BluetoothCommandCode.getCheckSendDataUtilsInstance().sendApplicationInfomationToWatchEND(mBluettothInterface);
//
//                }
//            },1000);


        } else if (BluetoothLeService.ACTION_CALL.equals(action)) {
            String style = intent.getStringExtra(BluetoothLeService.ACTION_CALL);
            if (style.equals(TelephonyManager.CALL_STATE_RINGING + "")) {//电话响铃
                Toast.makeText(App.context, "正在响铃000", Toast.LENGTH_LONG).show();
//                 Bundle mBundle = intent.getBundleExtra(BluetoothLeService.ACTION_NOTIFICATION);
//                 String appPackageName=mBundle.getString("appPackageName");
//                 String appNotificationTitle=mBundle.getString("appNotificationTitle");
//                 String appNotificationText=mBundle.getString("appNotificationText");
//                 Toast.makeText(App.context, "应用通知消息DIANHUA：\n"+appNotificationText, Toast.LENGTH_LONG).show();

//                 BluetoothCommandCode.getCheckSendDataUtilsInstance().sendPhoneCall(mBluettothInterface,TelephonyManager.CALL_STATE_RINGING);
                byte[] content = Myhex.hexStringToByte(Myhex.unicodeEncode("1234"));

                BluetoothCommandCode.getCheckSendDataUtilsInstance().sendApplicationInfomationToWatch(mBluettothInterface, content);

            } else if (style.equals(TelephonyManager.CALL_STATE_OFFHOOK + "")) {//电话接通

            } else if (style.equals(TelephonyManager.CALL_STATE_IDLE + "")) {//电话挂断

            }


        }
    }
}
